<?php

include_once ('bn_path.php');

class bn_upload
{
    protected static $instance;
    private $dl;
    private $conf;
    private $gf;
    
    function __construct()
    {
       $this->dl=new bn_dal; 
       $this->conf=bn_config::getInstance();
       $this->gf=bn_genralfunc::getInstance();
    }


    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function filename($file)
    {
        return $file['name'];
    }

    public function filesize($file)
    {
        return $file['size'];
    }

    public function filetype($file)
    {
        return $file['type'];
    }

    public function filetemp($file)
    {
        return $file['tmp_name'];
    }

    public function fileerror($file)
    {
        return $file['error'];
    }

    public function fileEXT($file)
    {
        return pathinfo($file['name'], PATHINFO_EXTENSION);
    }

    public function is_image($file)
    {
        $imageinfo = getimagesize($file['tmp_name']);
        if (empty($imageinfo['mime']))
            return false;
        else
            return true;
    }

    private function is_validfile($file)
    {
        if (strtolower($file['name']) == '.htaccess')
            return false;
        elseif (bn_check_fileEXT($file['name'],
            'php,htm,html,php4,php5,phps,jsp,aspx,ssh,py,pl,cgi,shtml,asp'))
            return false;
        else
            return true;
    }

    private function upload_file($file, $savefilename, $ext = '', $size = 0)
    {
        if ($savefilename != '')
        {
            if (isset($ext))
            {
                if (bn_check_fileEXT($file['name'], $ext) == false)
                    return 2;
            } elseif (self::is_validfile($file) == false)
                return 5;

            if ($size > 0)
            {
                if ($file['size'] > $size)
                    return "3 ".$file['size'];
            }

            if (is_dir($savefilename))
                $savefilename = bn_add_endpath_slash($savefilename) . $file['name'];
            move_uploaded_file($file['tmp_name'], $savefilename);
            return 1;
        } else
            return 4;
    }

    public function upload($file, $savefilename, $ext = null, $size = 0)
    {
        if (is_array($file['name']))
        {
            $files = self::multifilename($file);
            $savefilename = explode(',', $savefilename);
            $cnt = count($files);
            for ($i = 0; $i < $cnt; $i++)
            {
                $file = $files[$i];
                if (count($savefilename) == 1)
                    $ret[] = self::upload_file($file, $savefilename[0], $ext, $size);
                else
                    $ret[] = self::upload_file($file, $savefilename[$i], $ext, $size);

            }
            return $ret;
        } else
            return self::upload_file($file, $savefilename, $ext, $size);
    }

    public function multifilename($files)
    {
        if (isset($files))
        {
            $cnt = count($files['name']);
            $ret = array();
            for ($i = 0; $i < $cnt; $i++)
            {
                $file['name'] = $files['name'][$i];
                $file['size'] = $files['size'][$i];
                $file['type'] = $files['type'][$i];
                $file['tmp_name'] = $files['tmp_name'][$i];
                $file['error'] = $files['error'][$i];
                $ret[] = $file;
            }
            return $ret;
        }
    }
    
private function insert($user_id,$filename,$m_filename)
{
    $this->dl->insert('upload',"$user_id,$filename,$m_filename");
}

public function upload_user($file,$user_id,$ext)
{
    if($ext=='img')
    {
        if(self::is_image($file))
          {
            $filename=$this->gf->RandomStrgenerate().'.'.self::fileEXT($file);
            $id= self::upload($file,bn_get_dir_upload($filename),null,$this->conf->Get_value('img_size')) ;
            if($id==1)
            self::insert($user_id,$filename,self::filename($file));
            return $id;
          }
          else
          return 'file type is not image'; 
    }
    else
    {
        $id=self::upload($file,bn_get_dir_upload(),null,$this->conf->Get_value('file_size')) ;
        if($id==1)
        self::insert($user_id,$filename,self::filename($file));
        return $id;
    }

}

public function select_user($user_id)
{
    return $this->dl->select("select id,m_filename,concat('".bn_get_siteroot()."/files/upload/',filename)url from upload where user_id=?",$user_id);
}

public function delete($file_id)
{
    return $this->dl->delete('upload',$file_id);
}


}

?>